#include <FS.h>
#include "Arduino.h"
#include<string>

void listFS(){
    Dir dir = SPIFFS.openDir("/");
    while (dir.next()) {
        Serial.print("File: ");
        Serial.print(dir.fileName());
        Serial.print(" - Size: ");
        Serial.println(dir.fileSize());
    }
}

void writeFS()
{
    File file = SPIFFS.open("/config.txt", "w");
    if (file)
    {
        file.println("wifi_ssid=my_wifi");
        file.println("wifi_pwd=12345678");
        file.close();
        Serial.println("写入成功");
    }
    else
    {
        Serial.println("打开文件失败");
    }
}

void readFS()
{
    char *filePath[64];

    File file = SPIFFS.open("/config.txt", "r");
    if (file)
    {
        while (file.available())
        {
            String line = file.readStringUntil('\n');
            Serial.println(line);
        }
        file.close();
    }
    else
    {
        Serial.println("文件读取失败");
    }
}

void initFS()
{
    if (!SPIFFS.begin())
    {
        Serial.println("SPIFFS 初始化失败！");
        return;
    }

    Serial.println("SPIFFS 初始化成功！");

    FSInfo fs_info;
    SPIFFS.info(fs_info);
    Serial.printf("Total: %d bytes, Used: %d bytes\n", fs_info.totalBytes, fs_info.usedBytes);

    listFS();
}